package com.ruitu.mall_98_2.mine;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.utils.MD5Util;
import com.beanu.arad.utils.RegexUtils;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.view.IdentifyingCode;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.mall_98_2.mvp.contract.ForgetPasswordContract;
import com.ruitu.mall_98_2.mvp.model.ForgetPasswordModel;
import com.ruitu.mall_98_2.mvp.presenter.ForgetPasswordPresenter;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ForgetPasswordActivity extends MyBaseActivity<ForgetPasswordPresenter, ForgetPasswordModel> implements ForgetPasswordContract.View {
    private EditText et_forget_tel, et_forget_code, et_forget_password, et_forget_password_agin, et_iv_verifactiion_code;
    private TextView tv_get_code, tv_confirm_update;
    private ImageView iv_verifactiion_code;

    private boolean isGetSms = false;
    private int delayTime = 60;
    private String resultCode;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        String tel = et_forget_tel.getText().toString();
        String code = et_forget_code.getText().toString();
        String password = et_forget_password.getText().toString();
        String password_0 = et_forget_password_agin.getText().toString();
        if (tv_confirm_update == v) {
            if (!TextUtils.isEmpty(tel)) {
                if (!TextUtils.isEmpty(code)) {
                    if (RegexUtils.isPassword(password)) {
                        if (TextUtils.equals(password, password_0)) {
                            showProgress();
                            mPresenter.againRegister(tel, code, MD5Util.md5String(password));
                        } else {
                            ToastUtils.showShort("两次输入的密码不同");
                        }
                    } else {
                        ToastUtils.showShort("密码格式不正确");
                    }
                } else {
                    ToastUtils.showShort("验证码不能为空");
                }
            } else {
                ToastUtils.showShort("手机号不能为空");
            }

        }

        if (tv_get_code == v) {
            if (!TextUtils.isEmpty(tel)) {
                if (TextUtils.equals(et_iv_verifactiion_code.getText().toString().toLowerCase(), resultCode)) {
                    tv_get_code.setEnabled(false);
                    isGetSms = true;
                    setDelayTimeTextView(60);
                    handler.sendEmptyMessageDelayed(979, 1000);
                    getSmsCode(tel);
                } else {
                    ToastUtils.showShort("图片验证不正确");
                }
            } else {
                ToastUtils.showShort("手机号不能为空");
            }
            getVerifactionCode();
        }

        if (iv_verifactiion_code == v) {
            getVerifactionCode();
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
        setHeadTitle("忘记密码");
        et_forget_tel = findViewById(R.id.et_forget_tel);
        et_forget_code = findViewById(R.id.et_forget_code);
        et_forget_password = findViewById(R.id.et_forget_password);
        et_forget_password_agin = findViewById(R.id.et_forget_password_agin);
        tv_get_code = findViewById(R.id.tv_get_code);
        tv_confirm_update = findViewById(R.id.tv_confirm_update);
        iv_verifactiion_code = findViewById(R.id.iv_verifactiion_code);
        et_iv_verifactiion_code = findViewById(R.id.et_iv_verifactiion_code);
        setListener();
        getVerifactionCode();
    }

    private void setListener() {
        tv_get_code.setOnClickListener(this);
        tv_confirm_update.setOnClickListener(this);
        iv_verifactiion_code.setOnClickListener(this);
    }

    private void getVerifactionCode() {
        iv_verifactiion_code.setImageBitmap(IdentifyingCode.getInstance().createBitmap());
        resultCode = IdentifyingCode.getInstance().getCode().toLowerCase();
    }


    //获取验证码
    private void getSmsCode(String phone) {
        APIRetrofit.getDefault().smsSend(phone, "2")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        ToastUtils.showShort("获取验证码失败");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.isSuccess()) {
                            ToastUtils.showShort(baseResult.getMsg());

                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (isGetSms) {
                if (msg.what == 979) {
                    setDelayTimeTextView(--delayTime);
                    handler.sendEmptyMessageDelayed(979, 1000);
                }
            }
        }
    };

    private void setDelayTimeTextView(int i) {
        if (i == 0) {
            isGetSms = false;
            tv_get_code.setEnabled(true);
            tv_get_code.setText("获取验证码");
            delayTime = 60;
        } else {
            tv_get_code.setText(i + "s后重新获取");
//            tv_get_code.setEnabled(false);
        }
    }

    @Override
    public void setSuc() {
        finish();
    }
}
